const {randomInt, randomUUID} = require('crypto');
const ip = require('ip');

class Device {
    deviceinfo = null;
    client_ip = null;

    constructor() {
        const system = this.randomSystem();
        const [baseband, model] = this.randomBrand();
        this.deviceinfo = {
            network: 'wifi',
            screen_resolution: '1080x1920',
            system: system,
            electric: 100,
            processor_model: 'ARMv8 processor rev 1 (aarch64)',
            baseband: baseband,
            model: model,
            oaid: this.randomOaid(),
            adinfo: '',
            ad_device: '',
            'user-agent': this.randomUserAgent(system, model),
            ipua_md5: '',
        };

        this.client_ip = this.generateRandomChinaIP();
    }

    randomSystem() {
        return randomInt(9, 12).toString();
    }

    randomOaid() {
        return randomUUID({disableEntropyCache: true}).toString();
    }

    randomUserAgent(system, model) {
        return `Mozilla/5.0 (Linux; Android ${system}; ${model} Build/PQ3A.190705.11271837; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/91.0.4472.114 Mobile Safari/537.36; SHGR GaoreGame/2.4.3`;
    }

    randomBrand() {
        const maps = {
            HUAWEI: [
                'P30',
                'P30Pro',
                'Mate40Pro',
                'P50Pro',
                'MateX2',
                'P50E',
                'Mate50RS',
                'Nova10Pro',
                'Mate50Pro',
                'PocketS',
                'Mate40EPro',
                'Nova10SE',
            ],
            XIAOMI: [
                'Mi 10',
                'Mi 10 Pro',
                'Mi 10 Lite',
                'Mi 10T',
                'Redmi K40',
                'Redmi Note 9',
                'Redmi Note 10',
                'Redmi Note 11',
                'POCO M3',
                'POCO X3',
            ],
            VIVO: [
                'X60',
                'X60 Pro',
                'X50',
                'X50 Pro+',
                'Y70s',
                'Y51s',
                'S9',
                'S10',
                'T1',
                'T2x',
            ],
            OPPO: [
                'Reno4',
                'Reno4 Pro',
                'A93',
                'A94',
                'K7x',
                'K9x',
                'Find X3',
                'Find X3 Pro',
                'Ace1',
                'Ace2',
            ],
        };

        const key = Object.keys(maps)[randomInt(0, Object.keys(maps).length - 1)];
        const modes = maps[key];
        const model = modes[randomInt(0, modes.length - 1)];
        return [key, model];
    }

    generateRandomChinaIP() {
        const ip_segments = [
            '10.0.0.0/8',
            '172.16.0.0/12',
            '192.168.0.0/16',
            '100.64.0.0/10',
            '148.85.0.0/15',
            '162.152.0.0/14',
            '183.0.0.0/8',
            '185.129.0.0/16',
            '189.232.0.0/12',
            '203.0.113.0/24',
            '222.5.5.0/24',
            '233.39.0.0/16',
            '236.97.74.0/24',
            '249.155.0.0/24',
            '253.117.0.0/16',
        ];
        const randomIndex = randomInt(0, ip_segments.length - 1);
        const [str_ip_addr, str_ip_mask] = ip_segments[randomIndex].split('/');
        const ip_addr = ip.toLong(str_ip_addr);
        const mask = (0xffffffff << (32 - str_ip_mask)) >>> 0;
        const ip_addr_min = ip.fromLong(ip_addr & mask);
        const ip_addr_max = ip.fromLong(ip_addr | ~mask);
        return ip.fromLong(randomInt(ip.toLong(ip_addr_min), ip.toLong(ip_addr_max)));
    }
}

module.exports = Device